<?php

namespace app\index\controller;

use service\FeishuService;
use service\GuardService;
use service\SsoService;
use service\UtilService;
use think\Cache;
use think\Controller;
use think\Db;
use think\Session;

class Passport extends Controller
{

    public function _initialize()
    {

    }

    public function test(){
        $userInfo = Db::name("user")->where(['user_id'=>96005618])->find();
        session('homeuser_info', $userInfo);
        $homeuser_info = Session::get("homeuser_info");
        dump($homeuser_info);
        exit;
        $ctime = time();
        //FeishuService::instance()->GetuserBydeparmentid(94001269,$ctime);
        //exit;
        FeishuService::instance()->AddEditUser(15052754,"contact.user.updated_v3");exit;
        $main_department_lists = FeishuService::instance()->GetDeparmentlists();
        dump($main_department_lists);exit;
        exit;
        //$reslists = FeishuService::instance()->GetchildrendepByfdepid(50040477,time());
        //dump($reslists);exit;
        //FeishuService::instance()->GetuserBydeparmentid(50040478, time());exit;
        FeishuService::instance()->AddEditDepartment(50130594, "contact.department.updated_v3");exit;
        $userinfo = db('user')->where(['user_id'=>15000112])->find();
        $user = [
            "user_id" => $userinfo['user_id'],
            "mobile" => $userinfo['mobile'],
            "username" => $userinfo['username'],
            "gender" => $userinfo['gender'],
            //"avatar_origin" => $userinfo['avatar_origin'],
            "guardinfo" => "402888ee8e0db12d018e0db1b1930665"
        ];
        if($userinfo['is_jtzb'] == 1){
            $user['department_id'] = 50040478;
        }
        $res = GuardService::instance()->editUser($user);
        dump($res);exit;
        $userInfo = Db::name("user")->where(['user_id'=>96005618])->find();
        session('homeuser_info', $userInfo);
        $homeuser_info = Session::get("homeuser_info");
        dump($homeuser_info);

    }
    public function sso(){
        if(Session::has('homeuser_info.user_id')) {
            $this->redirect('index/index/index');
        }
        $uid = $this->request->param("uid",'','trim');
        $timestamp = $this->request->param("timestamp",'','trim');
        $token = $this->request->param("token",'','trim');

        if(SsoService::instance()->verify($uid,$timestamp,$token)){
            $userInfo = Db::name("user")->where(['mobile'=>$uid])->find();
            if(!empty($userInfo)){
                //echo "sso";
                session('homeuser_info', $userInfo); //保存数据到session
                //添加日志
                AddLogs(['describe' => 'SSO自动登录','admin_id'=>$userInfo['user_id'],'admin_name'=>$userInfo['username'],'browse'=>UtilService::getBrowser(),'login_time'=>date("Y-m-d H:i:s"),'type'=>3,'msg'=>json_encode($this->request->request(),304),'msg_in'=>json_encode(['openid'=>$userInfo['open_id']]),'msg_out'=>json_encode($userInfo,JSON_UNESCAPED_SLASHES),'server'=>json_encode($_SERVER,304)]);
                $this->redirect('/index/index/index');
            }
            else{
                AddLogs(['describe' => 'SSO自动登录失败，没有此用户！','browse'=>UtilService::getBrowser(),'login_time'=>date("Y-m-d H:i:s"),'type'=>3,'msg'=>json_encode($this->request->request(),304),'server'=>json_encode($_SERVER,304)]);
                exit("SSO自动登录失败，没有此用户！");
            }
        }
        else{
            AddLogs(['describe' => 'SSO自动登录失败，参数校验失败！','browse'=>UtilService::getBrowser(),'login_time'=>date("Y-m-d H:i:s"),'type'=>3,'msg'=>json_encode($this->request->request(),304),'server'=>json_encode($_SERVER,304)]);
            exit("SSO自动登录失败，参数校验失败！");
        }
    }

    public function ymtsso(){
        if(Session::has('homeuser_info.user_id')) {
            $this->redirect('index/index/ymt');
        }
        $uid = $this->request->param("uid",'','trim');
        $timestamp = $this->request->param("timestamp",'','trim');
        $token = $this->request->param("token",'','trim');

        if(SsoService::instance()->verify($uid,$timestamp,$token)){
            $userInfo = Db::name("user")->where(['mobile'=>$uid])->find();
            if(!empty($userInfo)){
                //echo "sso";
                session('homeuser_info', $userInfo); //保存数据到session
                //添加日志
                AddLogs(['describe' => 'SSO自动登录','admin_id'=>$userInfo['user_id'],'admin_name'=>$userInfo['username'],'browse'=>UtilService::getBrowser(),'login_time'=>date("Y-m-d H:i:s"),'type'=>3,'msg'=>json_encode($this->request->request(),304),'msg_in'=>json_encode(['openid'=>$userInfo['open_id']]),'msg_out'=>json_encode($userInfo,JSON_UNESCAPED_SLASHES),'server'=>json_encode($_SERVER,304)]);
                $this->redirect('/index/index/ymt');
            }
            else{
                AddLogs(['describe' => 'SSO自动登录失败，没有此用户！','browse'=>UtilService::getBrowser(),'login_time'=>date("Y-m-d H:i:s"),'type'=>3,'msg'=>json_encode($this->request->request(),304),'server'=>json_encode($_SERVER,304)]);
                exit("SSO自动登录失败，没有此用户！");
            }
        }
        else{
            AddLogs(['describe' => 'SSO自动登录失败，参数校验失败！','browse'=>UtilService::getBrowser(),'login_time'=>date("Y-m-d H:i:s"),'type'=>3,'msg'=>json_encode($this->request->request(),304),'server'=>json_encode($_SERVER,304)]);
            exit("SSO自动登录失败，参数校验失败！");
        }
    }

    public function login(){
        if(Session::has('homeuser_info.user_id')) {
            $this->redirect('index/index/index');
        }
        $code = $this->request->get('code','','trim');
        if(!empty($code)){
            $f_userinfo = FeishuService::instance()->GetUserinfo($code);
            if(isset($f_userinfo['user_id'])){
                $userInfo = Db::name("user")->where(['user_id'=>$f_userinfo['user_id']])->find();
                if(!empty($userInfo)){
                    session('homeuser_info', $userInfo); //保存数据到session
                    //添加日志
                    AddLogs(['describe' => '自动登录','admin_id'=>$userInfo['user_id'],'admin_name'=>$userInfo['username'],'browse'=>UtilService::getBrowser(),'login_time'=>date("Y-m-d H:i:s"),'type'=>3,'msg'=>json_encode($this->request->request(),304),'msg_in'=>json_encode(['openid'=>$userInfo['open_id']]),'msg_out'=>json_encode($userInfo,JSON_UNESCAPED_SLASHES),'server'=>json_encode($_SERVER,304)]);
                    $this->redirect('/index/index/index');
                }
            }
        }
        else{
            $this->redirect(FeishuService::instance()->GetLogincodeurl());
        }
    }

    public function logout(){
        //$homeuser_info = Session::get('homeuser_info');
        session('homeuser_info', null);
        $this->redirect('/index/index/index');exit;
    }
}